<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from 'echarts'
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '500px'
    }
  },
  data() {
    return {
      chart: null,
      myColor: ['#eb2100', '#eb3600', '#d0570e', '#d0a00e', '#34da62', '#00e9db', '#00c0e9', '#0096f3', '#33CCFF', '#33FFCC'],
      yAxisData: ['十三矿', '首山一矿', '范庄站', '八矿', '田庄站', '申西站', '中心站', '七矿', '苗圃站', '高庄站'],
      option: {},
      timer: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })

    this.timer = setInterval(() => {
      this.refreshData()
    }, 2000)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
    clearInterval(this.timer)
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption({
        grid: {
          left: '8%',
          top: '0%',
          right: '0%',
          bottom: '0%',
          containLabel: true
        },
        xAxis: [
          {
            show: false
          }
        ],
        yAxis: [
          {
            name: 'stationNmae',
            axisTick: 'none',
            axisLine: 'none',
            offset: '27',
            axisLabel: {
              textStyle: {
                color: '#ffffff',
                fontSize: '13'
              }
            },
            data: this.yAxisData
          },
          {
            axisTick: 'none',
            axisLine: 'none',
            axisLabel: {
              textStyle: {
                color: '#ffffff',
                fontSize: '10'
              }
            },
            data: ['10', '9', '8', '7', '6', '5', '4', '3', '2', '1']
          },
          {
            axisLine: {
              lineStyle: {
                color: 'rgba(0,0,0,0)'
              }
            },
            data: []
          }
        ],
        series: [
          {
            name: '条',
            type: 'bar',
            yAxisIndex: 0,
            data: [4, 13, 25, 29, 38, 44, 50, 52, 60, 72],
            label: {
              normal: {
                show: true,
                position: 'right',
                textStyle: {
                  color: '#ffffff',
                  fontSize: '12'
                }
              }
            },
            barWidth: 12,
            itemStyle: {
              normal: {
                color: params => {
                  var num = this.myColor.length
                  return this.myColor[params.dataIndex % num]
                }
              }
            },
            z: 2
          },
          {
            name: '白框',
            type: 'bar',
            yAxisIndex: 1,
            barGap: '-100%',
            data: [99, 99.5, 99.5, 99.5, 99.5, 99.5, 99.5, 99.5, 99.5, 99.5],
            barWidth: 20,
            itemStyle: {
              normal: {
                color: '#0e2147',
                barBorderRadius: 5
              }
            },
            z: 1
          },
          {
            name: '外框',
            type: 'bar',
            yAxisIndex: 2,
            barGap: '-100%',
            data: [100, 100, 100, 100, 100, 100, 100, 100, 100, 100],
            barWidth: 24,
            itemStyle: {
              normal: {
                color: params => {
                  var num = this.myColor.length
                  return this.myColor[params.dataIndex % num]
                },
                barBorderRadius: 5
              }
            },
            z: 0
          },
          {
            name: '外圆',
            type: 'scatter',
            hoverAnimation: false,
            data: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            yAxisIndex: 2,
            symbolSize: 28,
            itemStyle: {
              normal: {
                color: params => {
                  var num = this.myColor.length
                  return this.myColor[params.dataIndex % num]
                },
                opacity: 1
              }
            },
            z: 2
          }
        ]
      })
    },
    refreshData() {
      var i = Math.round(Math.random()) * 2
      var j = Math.round(Math.random()) * 3
      this.option = {
        // 系列中的数据内容数组。
        yAxis: [
          {
            name: 'stationNmae',
            data: this.yAxisData
          }
        ],
        series: [
          {
            name: '条',
            data: [4 + i, 13 + i, 25 + i, 29 + i, 38 + i, 44 + j, 50 + j, 52 + j, 60 + j, 72 + j]
          }
        ]
      }
      this.chart.setOption(this.option)
    }
  }
}
</script>
